package action;

import java.util.List;
import java.util.Map;

import model.ChangeInfoUser;
import model.ChangePassUser;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import pojo.Cart;
import pojo.CartDetail;
import pojo.Product;
import pojo.UserProfile;
import pojo.WishList;
import pojo.WishListDetail;

import com.opensymphony.xwork2.ActionSupport;

import dao.CartDAO;
import dao.ProductDAO;
import dao.UserDAO;
import dao.WishListDAO;

public class UserAction extends ActionSupport implements RequestAware, SessionAware {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = -4421839892626315418L;
	private Map<String, Object> request;
    private Map<String, Object> session;
    
    @Autowired  
    private UserDAO UserDAO; 
    @Autowired  
    private WishListDAO WishListDAO;
    @Autowired  
    private ProductDAO ProductDAO;
    @Autowired
	private CartDAO CartDAO;
    
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	
	private String action = "";
	private String button = "";
	private String sku = "";
	private String nameWishList = "";
	private String Description = "";

	private ChangePassUser changePass;
	private ChangeInfoUser changeInfoUser;
	
	private String idCart = "";
	private int sortby = 1;

	public String execute() {
		
		if (action == "" || action.equals("view"))
		{
			UserProfile user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
			request.put("user", user);
			return SUCCESS;
		}
		
		// Nhom chuc nang view cart
		if (action.equals("viewcart"))
		{
			List<Cart> lstCart = CartDAO.getListCartByEmail(( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(), sortby);
			request.put("lstCart", lstCart);
			return "viewcart";
		}
		if (action.equals("viewcartdetail") && idCart.isEmpty() == false)
		{
			Cart cart = CartDAO.getCartByID(idCart);
			request.put("cart", cart);
			return "viewcartdetail";
		}
		
		// Nhom chuc nang thay doi thong tin ca nhan
		if (action.equals("changeinformation") && button.equals("Update"))
		{
			UserProfile user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
			if (changeInfoUser == null || changeInfoUser.getFirstName() == null || changeInfoUser.getFirstName().isEmpty()
					|| changeInfoUser.getLastName() == null || changeInfoUser.getLastName().isEmpty()
					|| changeInfoUser.getPhoneNumber() == null || changeInfoUser.getPhoneNumber().isEmpty()
					|| changeInfoUser.getZipcode() == null || changeInfoUser.getZipcode().isEmpty() )
			{
				changeInfoUser.setEmail(user.getEmail());
				request.put("user", changeInfoUser);
				request.put("error", "All fields must be are required !!!");
				return SUCCESS;
			}
			
			user.setFirstName(changeInfoUser.getFirstName());
			user.setLastName(changeInfoUser.getLastName());
			user.setPhoneNumber(changeInfoUser.getPhoneNumber());
			user.setZipcode(changeInfoUser.getZipcode());
			
			if ( UserDAO.changeInfoUser(user) )
				request.put("success", "Update success !!!");
			else
				request.put("error", "Can not update information account !!!");
				
			request.put("user", user);
			return SUCCESS;
		}
		
		if (action.equals("changepass"))
		{
			if (button.equals("Update"))
			{
				if (changePass != null && changePass.getNewPass().equals(changePass.getNewPassReenter()))
				{
					if (UserDAO.changePasswordUser(( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(), changePass.getOldPass(), changePass.getNewPass()))
						request.put("success", "Change password success.");
					else
						request.put("error", "Can not change password.");
				}
				else
					request.put("error", "New password and new password reenter not match.");
				return "changepass";
			}
			else if (button.equals("Cancel"))
			{
				UserProfile user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
				request.put("user", user);
				return SUCCESS;
			}
			return "changepass";
		}
		// ============================================================
		
		// Nhom chuc nang ve Wish Lish
		if (action.equals("viewwishlist"))
		{
			WishList wishlist = WishListDAO.getWishListByEmail(( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
			request.put("wishlist", wishlist);
			return "viewwishlist";
		}
		if (action.equals("AddToWishList") && sku != "")
		{
			UserProfile user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
			if (user.getWishLists().iterator().hasNext())
			{
				if (ProductDAO.getProductBySKU(sku) != null)
				{
					WishListDAO.addProductToWishList((WishList)user.getWishLists().iterator().next(), ProductDAO.getProductBySKU(sku));
				}
				WishList wishlist = WishListDAO.getWishListByEmail(user.getEmail()); 
				request.put("wishlist", wishlist);
			}
			else
			{
				request.put("sku", sku);
				return "newwishlist";
			}
			return "viewwishlist";
		}
		if (action.equals("NewWishList") )
		{
			if (nameWishList == null || nameWishList.isEmpty())
			{
				if (sku != null)
					request.put("sku", sku);
				request.put("error", "Error");
				return "newwishlist";
			}
			
			UserProfile user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
			
			WishList wl = new WishList();
			wl.setName(nameWishList);
			wl.setMessage(Description);
			
			if (WishListDAO.addWishList(wl, user) == false)
			{
				if (sku != null)
					request.put("sku", sku);
				request.put("error", "Error");
				return "newwishlist";
			}
			
			if (sku != null)
			{
				if (ProductDAO.getProductBySKU(sku) != null)
				{
					user = UserDAO.getUserProfile( ( (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
					WishListDAO.addProductToWishList((WishList)user.getWishLists().iterator().next(), ProductDAO.getProductBySKU(sku));
				}
			}
			
			WishList wishlist = WishListDAO.getWishListByEmail(user.getEmail()); 
			request.put("wishlist", wishlist);
			return "viewwishlist";
		}
		if (action.equals("RemoveItemWishList") && sku != "")
		{
			String email = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(); 
			WishListDAO.deleteProductInWishList(WishListDAO.getWishListByEmail(email), ProductDAO.getProductBySKU(sku));
			WishList wishlist = WishListDAO.getWishListByEmail(email); 
			request.put("wishlist", wishlist);
			return "viewwishlist";
		}
		// ========================================================
        return SUCCESS;
    }
	
	
	
	
	
	
	
	
	
	
	
	
	public void setAction(String action) {
		this.action = action;
	}
	public String getSku() {
		return sku;
	}
	public void setSku(String sku) {
		this.sku = sku;
	}
	public String getNameWishList() {
		return nameWishList;
	}
	public void setNameWishList(String nameWishList) {
		this.nameWishList = nameWishList;
	}
	public String getDescription() {
		return Description;
	}
	public void setDescription(String description) {
		Description = description;
	}
	public String getButton() {
		return button;
	}
	public void setButton(String button) {
		this.button = button;
	}
	public ChangePassUser getChangePass() {
		return changePass;
	}
	public void setChangePass(ChangePassUser changePass) {
		this.changePass = changePass;
	}
	public ChangeInfoUser getChangeInfoUser() {
		return changeInfoUser;
	}
	public void setChangeInfoUser(ChangeInfoUser changeInfoUser) {
		this.changeInfoUser = changeInfoUser;
	}
	public String getIdCart() {
		return idCart;
	}
	public void setIdCart(String idCart) {
		this.idCart = idCart;
	}
	public int getSortby() {
		return sortby;
	}
	public void setSortby(int sortby) {
		this.sortby = sortby;
	}
}
